/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.swta;

import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.resources.StyleSheetReference;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.validation.validator.StringValidator.LengthBetweenValidator;

/**
 *
 * @author Jan
 */
public final class AddItemPackage extends WebPage {

    private WebMarkupContainer theContainer;
    private Form itemPackageForm;
    private ListView listView;
    private AddItemsToPackageModalWindow addItemWindow;
    private List<MyItem> packageContent = new ArrayList<MyItem>();
    private RequiredTextField titleField = new RequiredTextField("title");
    final FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
    private RequiredTextField idField = new RequiredTextField("id");

    MyItemPackage newItemPackage = new MyItemPackage();

    public AddItemPackage() {
        super();

        add(new StyleSheetReference("stylesheet", AddItem.class, "style.css"));
        this.itemPackageForm = new Form("itemPackageForm");

        this.itemPackageForm.setModel(new CompoundPropertyModel(new MyItemPackage()));

//        this.itemPackageForm.add(new DropDownChoice("category", CategoryManager.getInstance().getCategoryNames()));
        this.itemPackageForm.add(idField.add(new LengthBetweenValidator(3, 20)));
        this.itemPackageForm.add(titleField.add(new LengthBetweenValidator(3, 20)));
        this.itemPackageForm.add(this.feedbackPanel);

        this.itemPackageForm.add(new SubmitLink("submitLink") {

            @Override
            public void onSubmit() {

                MyItemPackage packageModel = (MyItemPackage) AddItemPackage.this.itemPackageForm.getModelObject();
                newItemPackage.setId(packageModel.getId());
                newItemPackage.setTitle(packageModel.getTitle());
                MyItemPackageManager.getInstance().addItemSet(AddItemPackage.this.newItemPackage);
                setResponsePage(MyItemPackages.class);
            }
        });

        Button cancelButton = new Button("cancelButton") {

            @Override
            public void onSubmit() {
                setResponsePage(MyItemPackages.class);
            }
        };
        cancelButton.setDefaultFormProcessing(false);
        this.itemPackageForm.add(cancelButton);


        this.theContainer = new WebMarkupContainer("theContainer");

//        this.itemPackageForm = new ItemPackageForm("itemPackageForm", this);
        add(itemPackageForm);
//        this.itemsPanel = new PackageItemsPanel("packageContent");
//        this.itemsPanel.setVisible(true);
//        add(this.itemsPanel);

        this.theContainer.setOutputMarkupId(true);

        this.listView = new ListView("allItems", this.newItemPackage.getItemList()) {

            @Override
            protected void populateItem(ListItem item) {
                MyItem actItem = (MyItem) item.getModelObject();

                item.add(new Label("id", actItem.getId()));
                item.add(new Label("name", actItem.getName()));
                item.add(new Label("weight", "" + actItem.getWeight()));
                item.add(new Label("unit", "" + actItem.getUnit()));
                item.add(new Label("price", "" + actItem.getPrice()));
            }
        };

        this.theContainer.add(this.listView);

        this.add(theContainer);

        this.addItemWindow = new AddItemsToPackageModalWindow("editWindow", this, this.newItemPackage) {

            @Override
            void onCancel(AjaxRequestTarget target) {

                close(target);
            }

            @Override
            void onSelect(AjaxRequestTarget target, String selection) {
//                AddItemPackage.this.addListView();
                System.out.println("ONSELECT!!!!!");
                System.out.println("Items: ");

                List<MyItem> l = AddItemPackage.this.newItemPackage.getItemList();

                for (int i = 0; i < l.size(); i++) {
                    System.out.println("I: " + l.get(i).getId());
                }
                AddItemPackage.this.newItemPackage = AddItemPackage.this.addItemWindow.getEditedItemPackage();

                AddItemPackage.this.listView.setList(AddItemPackage.this.newItemPackage.getItemList());
                target.addComponent(AddItemPackage.this.theContainer);

                close(target);
            }
        };

        this.add(new AjaxLink("addItem") {

            @Override
            public void onClick(AjaxRequestTarget target) {
                add(AddItemPackage.this.addItemWindow);
                AddItemPackage.this.addItemWindow.setPackage(AddItemPackage.this.newItemPackage);
                AddItemPackage.this.addItemWindow.show(target);
//                MyItemPackage sel = MyItemPackageManager.getInstance().getItemPackage(ItemPackageEditContentPanel.this.itemId);
//                target.appendJavascript("Wicket.Window.unloadConfirmation = false;");
//                ItemPackageEditContentPanel.this.addItemWindow.setTextMessage("Add/ Remove Items to " + sel.getTitle() + " (" + sel.getId() + ").");
//                ItemPackageEditContentPanel.this.addItemWindow.setTitleText("Edit of " + sel.getTitle());
////                ItemPackageEditContentPanel.this.selectedID = sel.getId();
//
//                ItemPackageEditContentPanel.this.addItemWindow.setPackageID(ItemPackageEditContentPanel.this.itemId);
////                ((ItemEditContentPanel) ItemActionPanel.this.editWindow.get(ItemActionPanel.this.editWindow.getContentId())).setItemToEdit(sel.getId());
//                ItemPackageEditContentPanel.this.addItemWindow.show(target);

            }
        });
        this.add(this.addItemWindow);
    }


}

